package com.lin.execute;

import com.lin.entity.Font;
import com.lin.entity.XmlFonts;
import com.lin.info.FontsInfo;
import com.net.Network;
import org.zjx.file.XmlUtils;

import java.util.ArrayList;
import java.util.List;

public class FontUtil {
    public static void save(Font font){
        final boolean[] wait = {true};
        Network network = new Network();
        network.saveFont(new Network.Callback() {
            @Override
            public void onResult(Object object) {
                wait[0] = false;
            }

            @Override
            public void onError(String error) {
                wait[0] = false;
            }
        }, font);

        while (wait[0]) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static List<Font> getFontList(String fontFilePath) {
        List<String> classPaths = new ArrayList<>();
        classPaths.add("com.lin.entity");
        org.zjx.file.XmlUtils.setSimpleClassName(true,classPaths);
        String xml = null;
        try {
            xml = FileUtil.getFileContent(fontFilePath);
            if (xml.isEmpty()) return null;
            XmlFonts fontsInfo = XmlUtils.xmlToObject(xml, XmlFonts.class);
            return fontsInfo.getFonts();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
